package com.business_clean.ui.activity.custom;

import androidx.lifecycle.Observer;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.business_clean.app.App;
import com.business_clean.app.base.BaseActivity;
import com.business_clean.app.uitl.ToastUtil;
import com.business_clean.data.mode.custom.CustomMangerList;
import com.business_clean.databinding.ActivityAddCustomBinding;
import com.business_clean.viewmodel.request.CustomViewModel;

import org.jetbrains.annotations.Nullable;

import java.util.HashMap;

import me.hgj.mvvmhelper.ext.AppExtKt;

public class AddCustomActivity extends BaseActivity<CustomViewModel, ActivityAddCustomBinding> {


    private String uuid;

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        if (getIntent() != null && getIntent().getExtras() != null) {
            CustomMangerList custom = (CustomMangerList) getIntent().getExtras().getSerializable("custom");
            if (custom != null) {
                uuid = custom.getUuid();
                mDatabind.tabbar.setTitle("编辑客户");
                mDatabind.withName.setInputText(custom.getCustom_name());
                mDatabind.withFullName.setInputText(custom.getCustom_full_name());
            } else {
                mDatabind.tabbar.setTitle("添加客户");
            }
        }
    }

    @Override
    public void onBindViewClick() {
        mDatabind.buttom.butBase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mDatabind.withFullName.getInputText())) {
                    ToastUtil.show("请输入公司全称");
                    return;
                }
                if (TextUtils.isEmpty(mDatabind.withName.getInputText())) {
                    ToastUtil.show("请输入公司简称");
                    return;
                }
                HashMap stringHashMap = new HashMap<String, String>();
                if (!TextUtils.isEmpty(uuid)) {
                    stringHashMap.put("uuid", "" + uuid);
                }
                stringHashMap.put("custom_full_name", "" + mDatabind.withFullName.getInputText());
                stringHashMap.put("custom_name", "" + mDatabind.withName.getInputText());
                mViewModel.saveCustomInfo(stringHashMap);
            }
        });
    }

    @Override
    public void initObserver() {
        mViewModel.getUpdate().observe(this, new Observer<Object>() {
            @Override
            public void onChanged(Object o) {
                ToastUtil.show("操作成功");
                App.appViewModelInstance.getRefreshCustomManagerList().setValue(true);
                finish();
            }
        });
    }
}